<script setup>
import { Edit, Delete } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue'
import pageConrtainer from '@/components/pageConrtainer.vue'
import { getArticleCatagoryAPI, deleteArticleCatagoryAPI } from '@/api/article'
import EditArticle from './components/editArticle.vue'
// 文章分类列表
const articleCategoryList = ref([])

// 获取文章列表
const getArticleCategoryList = async () => {
  isLoading.value = true
  const res = await getArticleCatagoryAPI()
  console.log(res)
  articleCategoryList.value = res.data.data
  isLoading.value = false
}

// 初始化调用
onMounted(() => {
  getArticleCategoryList()
})

// 加载控制
const isLoading = ref(true)

// 添加分类
const addCategory = () => {
  editArticleRef.value.open(false)
}

// 弹层组件对象
const editArticleRef = ref(null)

// 编辑分类
const editCategory = (row) => {
  editArticleRef.value.open(row)
}

// 删除文章分类
const handleDelete = (id) => {
  ElMessageBox.confirm('确定删除该分类吗？', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const res = await deleteArticleCatagoryAPI(id)
      // 提示用户
      ElMessage.success(res.data.message)
      // 重新渲染
      getArticleCategoryList()
    })
    .catch(() => {})
}

// 添加编辑成功
const onSuccess = () => {
  getArticleCategoryList()
}
</script>
<template>
  <pageConrtainer style="width: 100%" title="文章分类">
    <template #extra>
      <el-button type="primary" @click="addCategory">添加分类</el-button>
    </template>
    <el-table
      v-loading="isLoading"
      :data="articleCategoryList"
      style="width: 100%"
    >
      <!-- 表格内容 -->
      <el-table-column
        type="index"
        label="序号"
        width="100px"
      ></el-table-column>
      <el-table-column prop="cate_name" label="文章分类"></el-table-column>
      <el-table-column prop="cate_alias" label="文章别名"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            circle
            :icon="Edit"
            type="primary"
            @click="editCategory(row)"
          ></el-button>
          <el-button
            circle
            :icon="Delete"
            type="danger"
            @click="handleDelete(row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <EditArticle @success="onSuccess" ref="editArticleRef"></EditArticle>
  </pageConrtainer>
</template>

<style lang="scss" scoped></style>
